package main

import (
	"bufio"
	_ "bufio"
	"fmt"
	"net"
	"os"
	_ "os"
	"time"
)

func recv(conn net.Conn) {
	buf := make([]byte, 1024)
	recv, err := conn.Read(buf) // 读取
	fmt.Println(recv, err)
	if err != nil {
		return
	}
	fmt.Println("接收内容：", string(buf[:recv]))
}

func main() {

	for {

		//conn, err := net.Dial("tcp", "127.0.0.1:8000")
		//使用带超时的连接
		//time.Sleep(time.Second*5)
		conn, err := net.DialTimeout("tcp", "127.0.0.1:8000", time.Second*5)
		if err != nil {
			fmt.Println("client dial err", err)
			return
		}
		defer conn.Close()
		// 从终端输入
		//reader := bufio.NewReader(os.Stdin)  // os.Stdin终端标准输入
		//line, err := reader.ReadString('\n')
		//if err!=nil{
		//	fmt.Println(err)
		//}
		fmt.Printf("client send:")
		inputs := bufio.NewReader(os.Stdin)
		data, _ := inputs.ReadString('\n')
		var p string
		m, _ := inputs.Read([]byte(p))
		//fmt.Println("======",m)
		//fmt.Println("----", data)
		if m == 0 {
			fmt.Println("请输入内容")
			continue
		}
		n, err := conn.Write([]byte(data)) // 将line发送给服务器
		if err != nil {
			fmt.Println("write error", err)
		}
		fmt.Println("发送字节数", n)
		buf := make([]byte, 1024)
		recv, err := conn.Read(buf) // 读取
		fmt.Println(recv, err)
		if err != nil {
			return
		}
		fmt.Println("接收内容:", string(buf[:recv]))
		//go recv(conn)
	}
}
